<template>
  <el-dialog
    :title="'调拨'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">
      <el-form-item label="当前冷库">
        <el-input v-model="dataForm.oldStorageName" placeholder="当前冷库" disabled></el-input>
      </el-form-item>
      <el-form-item label="调拨至冷库" prop="storageId">
        <el-select filterable v-model="dataForm.newStorageId" placeholder="请选择">
          <el-option @click.native="storage(dataForm.newStorageId)"
            v-for="item in storageOptions"
            :key="item.id"
            :label="item.storageName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="调拨至库位" prop="spaceId" v-if="spaceValile">
        <el-select filterable v-model="dataForm.newSpaceId" placeholder="请选择">
          <el-option v-for="item in spaceOptions"
                     :key="item.id"
                     :label="item.spaceName"
                     :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="调拨数量" prop="area">
        <el-input-number v-model="dataForm.commodityNum" placeholder="调拨数量"></el-input-number>
      </el-form-item>
      <el-form-item label="调拨面积" prop="area">
        <el-input-number v-model="dataForm.area" placeholder="面积">平方米</el-input-number>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        defaultCascaderValue: [], // 默认值
        myProp: {
          value: 'value',
          label: 'label',
          checkStrictly: true
        },
        options: [],
        optionsCustomer: [],
        storageOptions: [],
        spaceOptions: [],
        spaceValile: false,
        visible: false,
        dataForm: {
          id: 0,
          commodityBatch: '',
          commodityName: '',
          oldStorageId: '',
          oldStorageName: '',
          oldSpaceId: '',
          oldSpaceName: '',
          newStorageId: '',
          newStorageName: '',
          newSpaceId: '',
          newSpaceName: '',
          commodityNum: '',
          area: ''
        },
        dataRule: {
        }
      }
    },
    created () {
      this.getStorageList()
    },
    methods: {
      storage (data) {
        debugger
        this.spaceValile = true
        this.getStorageSpace(data)
      },
      getStorageSpace (data) {
        this.$http({
          url: this.$http.adornUrl(`/app/entstoragespace/queryStorageSpace/${data}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.spaceOptions = data.list
          }
        })
      },
      getStorageList () {
        this.$http({
          url: this.$http.adornUrl(`/app/entcoldstorage/storageName/list`),
          method: 'get',
          params: this.$http.adornParams({
            ...this.dataForm
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.storageOptions = data.coldStorageList
          }
        })
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/app/entinventory/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.commodityBatch = data.entInventory.commodityBatch
                this.dataForm.commodityName = data.entInventory.commodityName
                this.dataForm.oldStorageId = data.entInventory.storageId
                this.dataForm.oldStorageName = data.entInventory.storageName
                this.dataForm.oldSpaceId = data.entInventory.spaceId
                this.dataForm.oldSpaceName = data.entInventory.spaceName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/app/entallocation/save`),
              method: 'post',
              data: this.$http.adornData({
                'commodityBatch': this.dataForm.commodityBatch,
                'commodityName': this.dataForm.commodityName,
                'newStorageId': this.dataForm.newStorageId,
                'newSpaceId': this.dataForm.newSpaceId,
                'oldStorageId': this.dataForm.oldStorageId,
                'oldStorageName': this.dataForm.oldStorageName,
                'oldSpaceId': this.dataForm.oldSpaceId,
                'oldSpaceName': this.dataForm.oldSpaceName,
                'commodityNum': this.dataForm.commodityNum,
                'area': this.dataForm.area
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
